package com.infindo.appcreate.zzyj.service;

 
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.Type;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import com.infindo.appcreate.zzyj.entity.Activity;
import com.infindo.appcreate.zzyj.entity.ActivityTalk;
import com.infindo.appcreate.zzyj.entity.Expert;
import com.infindo.appcreate.zzyj.entity.Infomation;
import com.infindo.appcreate.zzyj.entity.Project;
import com.infindo.appcreate.zzyj.entity.ProjectComment;
import com.infindo.appcreate.zzyj.entity.ProjectSupporter;
import com.infindo.framework.base.service.BaseServiceImpl;
import com.infindo.framework.base.vo.TableModel;
import com.infindo.framework.common.utils.PaginationSupport;

@Service("zzyjSpiderService")
public class ZzyjSpiderServiceImpl<T>   extends BaseServiceImpl implements ZzyjSpiderService {
     
    @Override
    public void deleteAllData(String ... tables){
            
           try {
               for(String table:tables){
                   
                   super.deleteAll(super.findAll(Class.forName("com.infindo.appcreate.zzyj.entity."+table)));
               }
               
           } catch (ClassNotFoundException e) {
               e.printStackTrace();
           }
        
    }

    @Override
    public void saveSpiderData(List ... dataList) {

        this.deleteAllData("Project","Expert","ExpertProject","ProjectComment","ProjectSupporter","Service","Activity","ActivityTalk","Infomation","ProjectHot");
        for(List<?> objList :dataList){
            for(Object obj :objList){
                super.save(obj);
            }
        }
    }
     
    @Override
    public void updateSpideData(List voList) {
             
        if(voList.size() > 0){
            this.saveSpiderData(voList);
        }
    }
     
    @Override
    public List<Project> getProjListByMcode(String mCode) {
        
        DetachedCriteria dc = DetachedCriteria.forClass(Project.class);
        
        int mCodeInt = Integer.valueOf(mCode);
        switch(mCodeInt){
             
            case 11://time
                dc.addOrder(Order.desc("time"));
                break;
            case 12://score
                dc.addOrder(Order.desc("score"));
                break;
            case 13://expert
                dc.addOrder(Order.desc("expertScore"));
                break;
            case 14://public
                dc.addOrder(Order.desc("crowdScore"));
                break;
            case 15://hot
                //dc.addOrder(Order.desc("crowdScore"));
                String hql = "select p from Project p, ProjectHot ph where p.code = ph.projectCode order by ph.id ";
                return super.find(hql);
             default:
                 dc.addOrder(Order.desc("time"));
                 break;
        }
        
        return findByCriteria(dc);
    }

    @Override
    public List<Project> getProjsByPage(String mCode, int pageSize, Integer startIndex) {
        DetachedCriteria dc = DetachedCriteria.forClass(Project.class);
        int mCodeInt = Integer.valueOf(mCode);
        switch(mCodeInt){
             
            case 11://time
                dc.addOrder(Order.desc("time"));
                break;
            case 12://score
                dc.addOrder(Order.desc("score"));
                break;
            case 13://expert
                dc.addOrder(Order.desc("expertScore"));
                break;
            case 14://public
                dc.addOrder(Order.desc("crowdScore"));
                break;
            case 15://host
                String hql = "select p from Project p, ProjectHot ph where p.code = ph.projectCode order by ph.id ";
                return super.find(hql);
             default:
                 dc.addOrder(Order.desc("time"));
                 break;
        }
        return findByCriteria(dc,pageSize,startIndex);
    }

   

    @Override
    public List<ProjectSupporter> getProjSupListByCodes(String proCode, Integer type) {
        DetachedCriteria dc = DetachedCriteria.forClass(ProjectSupporter.class);
        dc.add(Restrictions.eq("projectCode", proCode));
        
        switch(type){
            case 0://expert
                dc.add(Restrictions.eq("type", 0));
                break;
            case 1://public
                dc.add(Restrictions.eq("type", 1));
                break;
             default:
                 dc.add(Restrictions.eq("type", 0));
                 break;
        }
        dc.addOrder(Order.desc("time"));
        return findByCriteria(dc);
    }
    
    @Override
    public List<ProjectSupporter> getProjSupListByCodesPage(String proCode, Integer type,int pageSize, Integer startIndex) {
        DetachedCriteria dc = DetachedCriteria.forClass(ProjectSupporter.class);
        dc.add(Restrictions.eq("projectCode", proCode));
        
        switch(type){
            case 0://expert
                dc.add(Restrictions.eq("type", 0));
                break;
            case 1://public
                dc.add(Restrictions.eq("type", 1));
                break;
             default:
                 dc.add(Restrictions.eq("type", 0));
                 break;
        }
        dc.addOrder(Order.desc("time"));
        return findByCriteria(dc,pageSize,startIndex);
    }

   

    @Override
    public int getObjCount(Object t) {
        DetachedCriteria dc = DetachedCriteria.forClass(t.getClass());
        return this.getCountByCriteria(dc);
    }

    @Override
    public List<ProjectComment> getProjComtListByCodes(String proCode, Integer type) {
        DetachedCriteria dc = DetachedCriteria.forClass(ProjectComment.class);
        dc.add(Restrictions.eq("projectCode", proCode));
        
        switch(type){
            case 0://expert
                dc.add(Restrictions.eq("type", 0));
                break;
            case 1://public
                dc.add(Restrictions.eq("type", 1));
                break;
             default:
                 dc.add(Restrictions.eq("type", 0));
                 break;
        }
        dc.addOrder(Order.desc("time"));
        return findByCriteria(dc);
    }

    @Override
    public List<ProjectComment> getCommentsByPage(String proCode, Integer type, int pageSize, Integer startIndex) {
        DetachedCriteria dc = DetachedCriteria.forClass(ProjectComment.class);
        dc.add(Restrictions.eq("projectCode", proCode));
        
        switch(type){
            case 0://expert
                dc.add(Restrictions.eq("type", 0));
                break;
            case 1://public
                dc.add(Restrictions.eq("type", 1));
                break;
             default:
                 dc.add(Restrictions.eq("type", 0));
                 break;
        }
        dc.addOrder(Order.desc("time"));
        return findByCriteria(dc,pageSize,startIndex);
    }

    @Override
    public List<T> getObjList(Class class1) {
        DetachedCriteria dc = DetachedCriteria.forClass(class1);
        return findByCriteria(dc);
    }

    @Override
    public List<Expert> getExpertList() {
        DetachedCriteria dc = DetachedCriteria.forClass(Expert.class);
        dc.addOrder(Order.asc("id"));
        return findByCriteria(dc);
    }

    @Override
    public List<Expert> getExpertsByPage(int pageSize, Integer startIndex) {
        DetachedCriteria dc = DetachedCriteria.forClass(Expert.class);
        dc.addOrder(Order.asc("id"));
        return findByCriteria(dc,pageSize,startIndex);
    }

    @Override
    public List<Project> getExpertProjsList(String expertCode, int type) {
        String hql = "select p from Project p,ExpertProject ep where p.code = ep.projectCode and ep.expertCode = ? and ep.type = ? order by ep.id asc ";
        return super.find(hql, new Object[]{expertCode,type});
    }

    @Override
    public List getExpertComtsList(String expertCode) {
        String hql = "select new com.infindo.appcreate.zzyj.entity.ExpertCommentVO(p.code, p.title, pc.desc)  from Project p, ProjectComment pc where pc.expertCode = ? and p.code = pc.projectCode order by pc.time desc ";
        return super.find(hql, new Object[]{expertCode});
    }

	@Override
	public List<Activity> getActivityList() {
		DetachedCriteria dc = DetachedCriteria.forClass(Activity.class);
        dc.addOrder(Order.desc("time"));
        return findByCriteria(dc);
	}

	@Override
	public List<ActivityTalk> getActivityTalks(String actCode) {
		DetachedCriteria dc = DetachedCriteria.forClass(ActivityTalk.class);
		dc.add(Restrictions.eq("actCode", actCode));
        dc.addOrder(Order.desc("time"));
        return findByCriteria(dc);
	}

	@Override
	public List<Infomation> getInfomationList() {
		DetachedCriteria dc = DetachedCriteria.forClass(Infomation.class);
        dc.addOrder(Order.desc("time"));
        return findByCriteria(dc);
	}

	@Override
	public List<Infomation> getInfomatinsByPage(int pageSize, Integer startIndex) {
		DetachedCriteria dc = DetachedCriteria.forClass(Infomation.class);
		dc.addOrder(Order.desc("time"));
        return findByCriteria(dc,pageSize,startIndex);
	}

    
}
